Golang : Convert a rune to unicode style string \u
Continuing from previous tutorial on how to get escape characters or \u unicode style string. These are some additional methods to convert rune to unicode style string.
package main
import (
"fmt"
)
func main() {
// use %x for single rune
fmt.Printf("\\u%x\n", 'お')
// fmt.Printf("\\u%x\n", 'おはよう') -- will not work
// use %+q for multiple runes
fmt.Printf("%+q\n", "おはよう")
}
output :
\u304a
"\u304a\u306f\u3088\u3046"
Another way :
package main
import (
"fmt"
)
func main() {
u := fmt.Sprintf("%U", 'お')
fmt.Println(u)
}
Output :
U+304A
or use strconv.QuoteRuneToASCII()
function.
package main
import (
"fmt"
"strconv"
)
func main() {
// translate character to rune
r := rune('お')
// extra the unicode \uxxxx value
unicode := strconv.QuoteRuneToASCII(r)
fmt.Println(string(r)+" unicode string value is : ", unicode)
}
Output :
お unicode string value is : '\u304a'
Reference :
See also : Golang : Get escape characters \u form from unicode characters
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+12k Golang : GTK Input dialog box examples
+14.7k Golang : Rename directory
+10.3k Golang : Print how to use flag for your application example
+4.5k Linux/MacOSX : Search and delete files by extension
+40.4k Golang : UDP client server read write example
+9.6k Mac OSX : Get a process/daemon status information
+11.4k CodeIgniter : How to check if a session exist in PHP?
+11.8k Golang : Concurrency and goroutine example
+19.4k Golang : Check if directory exist and create if does not exist
+8.9k Golang : Random integer with rand.Seed() within a given range
+9k Golang : Find network service name from given port and protocol
+5.8k Golang : Error handling methods